package com.nuanshui.heatedloan.controller.base.file;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.RequestContext;

/* * 图片控制器
 * @ClassName: ImageController 
 * @author guohao
 * @date 2015年4月21日 上午9:18:56 
 *
 */
import com.nuanshui.heatedloan.enums.FileBelong;
import com.nuanshui.framework.exception.BizException;
import com.nuanshui.framework.exception.GlobalErrorCode;
import com.nuanshui.framework.res.ResourceFacade;
import com.nuanshui.heatedloan.controller.BaseController;

@Controller
public class ImageController extends BaseController {
	
	@Autowired
	private ResourceFacade resourceFacade;

	
	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		//绑定图片验证器
		binder.setValidator(new ImageUploadValidator());
	}
	/**
	 * 进入图片上传界面
	 * @param 
	 * @author guohao
	 * @date 2015年4月21日 上午11:49:04
	 */
	@RequestMapping("imgUpload")
	public String imgUpload(Model model){
		model.addAttribute("belong", FileBelong.OTHER);
		return "fileupload/imgUpload";
	}
	/**
	 * 上传图片
	 * @param 
	 * @author guohao
	 * @date 2015年4月21日 上午9:17:42
	 */
	@ResponseBody //前台：th:action="'_f/u?'+${_csrf.parameterName}+'='+${_csrf.token}"
	@RequestMapping(value = ResourceFacade.RES_PREFIX_UP_FILE + "/u", method = RequestMethod.POST)
	public String upload(@Valid @ModelAttribute ImageUploadForm form, Errors errors, HttpServletRequest request)
			throws Exception {
		if(form.getBelong() == null){
			RequestContext requestContext = new RequestContext(request);
			throw new BizException(GlobalErrorCode.UNKNOWN, requestContext.getMessage("valid.fileBelong.message"));
		}
		return success();
	}
}
